#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>

int main(int argc, char const *argv[])
{
    int fd = socket(AF_INET, SOCK_STREAM, 0);

    // 获取缓冲区大小
    int rev_size = 0;
    int rev_len = sizeof(rev_size);
    int send_size = 0;
    int send_len = sizeof(send_size);

    getsockopt(fd, SOL_SOCKET, SO_RCVBUF, &rev_size, &rev_len);
    getsockopt(fd, SOL_SOCKET, SO_SNDBUF, &send_size, &send_len);

    printf("rev_size = %d\n", rev_size);
    printf("send_size = %d\n", send_size);

    // 设置缓冲区大小
    int new_rev_size = 1024*8; // 设置接收缓冲区大小，内核会自动翻倍
    int new_send_size = 1024*8; // 设置发送缓冲区大小，内核会自动翻倍

    setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &new_rev_size, sizeof(new_rev_size));
    setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &new_send_size, sizeof(new_send_size));

    getsockopt(fd, SOL_SOCKET, SO_RCVBUF, &rev_size, &rev_len);
    getsockopt(fd, SOL_SOCKET, SO_SNDBUF, &send_size, &send_len);
    printf("rev_size = %d\n", rev_size);
    printf("send_size = %d\n", send_size);

    return 0;
}
